#ifndef __MESH_H__
#define __MESH_H__

#include <dalelib/v3.h>

namespace dalelib
{

class Mesh
{
private:

	GLuint _num_vertices;
	GLfloat* _vertices;
	GLfloat* _texcoords;
	GLfloat* _colours;
	GLuint _draw_mode;

public:

	Mesh(const int _NumVertices=0)
	{
		create(_NumVertices);
	}

	~Mesh() { destroy(); }

	void destroy()
	{
		if(_vertices) delete [] _vertices;
		if(_texcoords) delete [] _texcoords;
		if(_colours) delete [] _colours;
	}

	void create(const int _NumVertices)
	{
		_draw_mode = GL_TRIANGLES;
		_num_vertices = _NumVertices;
		_vertices = new GLfloat[_num_vertices*3];
//		_texcoords = new GLfloat[_num_vertices*2];
//		_colours = new GLfloat[_num_vertices*4];
	}

	GLuint getNumVertices() { return _num_vertices; }
	GLfloat* getVertices() { return _vertices; }
	GLfloat* getTeCoords() { return _texcoords; }
	GLfloat* getColours() { return _colours; }
	GLuint& getDrawMode() { return _draw_mode; }

	void draw()
	{
		if( _vertices )
		{
			glVertexPointer( 3, GL_FLOAT, 0, _vertices );
			glEnableClientState( GL_VERTEX_ARRAY );
		}

		if( _texcoords )
		{
			glTexCoordPointer( 2, GL_FLOAT, 0, _texcoords );
			glEnableClientState( GL_TEXTURE_COORD_ARRAY );
		}

		if( _colours )
		{
			glColorPointer( 4, GL_FLOAT, 0, _colours );
			glEnableClientState( GL_COLOR_ARRAY );
		}

		glDrawArrays( _draw_mode, 0, _num_vertices );
		glDisableClientState( GL_VERTEX_ARRAY );
		glDisableClientState( GL_TEXTURE_COORD_ARRAY );
		glDisableClientState( GL_COLOR_ARRAY );
	}

	void scale(const v3 _Scl)
	{
		int vindex = 0;
		for(int i=0; i<_num_vertices; ++i)
		{
			_vertices[vindex++] *= _Scl.x(); _vertices[vindex++] *= _Scl.y(); _vertices[vindex++] *= _Scl.z();
		}
	}

	void createCone(const int _Slices)
	{
		destroy();
		create(_Slices+2);

		int vindex = 0;
		_vertices[vindex++] = 0.f; _vertices[vindex++] = 1.f; _vertices[vindex++] = 0.f;
		for(int i=0; i<=_Slices; ++i)
		{
			float f = 3.14259f*2.f*(float)i/_Slices;
			_vertices[vindex++] = sinf(f); _vertices[vindex++] = 0.f; _vertices[vindex++] = cosf(f);
		}
	}

};

} // namespace dalelib


#endif
